#include "splashscreen.h"

#include <QPixmap>
#include <QVBoxLayout>
#include <QApplication>
#include <QFont>
#include <QPainter>
#include <QDateTime>
#include "./ui_splashscreen.h"

SplashScreen::SplashScreen(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SplashScreen)
{
    ui->setupUi(this);

    // 设置窗口属性
    setWindowFlags(Qt::SplashScreen | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, false);

    //软件名称文本标签
    ui->m_softwareNameLabel->setText(QString("Code Master %1").arg(QDateTime::currentDateTime().date().year()));
    ui->m_softwareNameLabel->setStyleSheet("color: #ffffff; font-size: 48px;");

    // 创建进度条
    ui->m_progressBar->setRange(0, 100);
    ui->m_progressBar->setValue(0);
    ui->m_progressBar->setStyleSheet(
        "QProgressBar {"
        "    border: 1px solid #cccccc;"
        "    border-radius: 3px;"
        "    text-align: center;"
        "    height: 8px;"
        "}"
        "QProgressBar::chunk {"
        "    background-color: #2196F3;"
        "    border-radius: 2px;"
        "}"
        );

    // 状态标签
    ui->m_statusLabel->setText("准备启动...");
    ui->m_statusLabel->setStyleSheet("color: #ffffff; font-size: 12px;");

    // 版本信息标签
    this->ui->m_versionLabel->setStyleSheet("color: #666666; font-size: 10px;");
    this->ui->m_versionLabel->setText(QString("Version: %1").arg(qApp->applicationVersion()));
}

void SplashScreen::setProgress(int value)
{
     ui->m_progressBar->setValue(value);
}

void SplashScreen::setStatusText(const QString &text)
{
    ui->m_statusLabel->setText(text);
}

void SplashScreen::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    // 设置启动界面背景图片
    QPixmap pixmap(":/images/splash_bg.png");
    QPainter painter(this);
    // 绘制背景图片，拉伸以填充整个窗口
    painter.drawPixmap(rect(), pixmap);
}
